const RongRTC = require('../../libs/RongRTC-wechat-minip-3.2.3');
const { ObserverList } = require('../../pages/common.js');
const utils = require('../../libs/undescore-1.8.3.js');
let STREAM_TAG = 'RongCloudRTC';

module.exports = (config, RongIMLib) => {
  let memberWatcher = new ObserverList();
  let rongRTC = new RongRTC({
    RongIMLib: RongIMLib,
    url: config.MS_URL,
    // debug: true,
    error: function(error){
      memberWatcher.notify({
        type: 'error',
        error
      });
    }
  });
  let { StreamType } = RongRTC;
  let { Room, Stream } = rongRTC;
  let $room = null, $stream = null;

  let memberWatch = (watcher) => {
    var force = true;
    memberWatcher.add(watcher, force);
  };
  let streamWatcher = new ObserverList();
  let streamWatch = (watcher) => {
    var force = true;
    streamWatcher.add(watcher, force);
  };
  let init = (room) => {
    let { id } = room;
    $room = new Room({
      id: id,
      joined: function (user) {
        memberWatcher.notify({
          type: 'joined',
          user
        });
      },
      left: function (user) {
        memberWatcher.notify({
          type: 'left',
          user
        });
      }
    });
    $stream = new Stream({
      published: function (user) {
        $stream.subscribe(user).then((user) => {
          streamWatcher.notify({
            type: 'published',
            user
          });
        });
      },
      unpublished: function (user) {
        $stream.unsubscribe(user).then(function () {
          streamWatcher.notify({
            user,
            type: 'unpublished'
          });
        });
      },
      disabled: function (user) {
        streamWatcher.notify({
          type: 'disabled',
          user
        });
      },
      enabled: function (user) {
        streamWatcher.notify({
          type: 'enabled',
          user
        });
      },
      muted: function (user) {
        streamWatcher.notify({
          type: 'muted',
          user
        });
      },
      unmuted: function (user) {
        streamWatcher.notify({
          type: 'unmuted',
          user
        });
      }
    });
  };
  let join = (user) => {
    return $room.join(user).then(() => {
      return $stream.publish({
        id: user.id,
        stream: {
          type: user.mediaType,
          tag: STREAM_TAG
        }
      }).then((_user) => {
        streamWatcher.notify({
          type: 'published',
          isSelf: true,
          user: _user
        })
      })
    })
  };
  let leave = (user) => {
    return $room.leave(user)
  };
  let mute = (user) => {
    let { audio } = $stream;
    return audio.mute({
      id: user.id,
      stream: {
        tag: STREAM_TAG
      }
    });
  };
  let unmute = (user) => {
    let { audio } = $stream;
    return audio.unmute({
      id: user.id,
      stream: {
        tag: STREAM_TAG
      }
    });
  };
  let disable = (user) => {
    let { video } = $stream;
    return video.disable({
      id: user.id,
      stream: {
        tag: STREAM_TAG
      }
    });
  };
  let enable = (user) => {
    let { video } = $stream;
    return video.enable({
      id: user.id,
      stream: {
        tag: STREAM_TAG
      }
    });
  };
  return {
    init,
    join,
    leave,
    mute,
    unmute,
    disable,
    enable,
    memberWatch,
    streamWatch
  };
};